package com.example.androidexample.flutter;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.example.androidexample.R;
import com.example.androidexample.qr.QRActivity;
import com.example.basic.base.BaseActivity;

import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class FlutterPageByFlutterFragmentActivity extends BaseActivity {

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        // 通过FlutterFragment引入Flutter编写的页面
        //方式一
//        FlutterFragment flutterFragment = FlutterFragment.createDefault();
//        getSupportFragmentManager()
//                .beginTransaction()
//                .add(R.id.fl_container, flutterFragment)
//                .commit();
        //方式二
//        FlutterFragment flutterFragment = FlutterFragment.withNewEngine()
//                .initialRoute("/")
//                .build();
//        getSupportFragmentManager()
//                .beginTransaction()
//                .add(R.id.fl_container, flutterFragment)
//                .commit();
        //方式三
        // 创建可缓存的FlutterEngine对象
        FlutterEngine flutterEngine = new FlutterEngine(this);
        flutterEngine.getNavigationChannel().setInitialRoute("/");
        flutterEngine.getDartExecutor().executeDartEntrypoint(
                DartExecutor.DartEntrypoint.createDefault()
        );
        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);

// 通过FlutterFragment引入Flutter编写的页面
        FlutterFragment flutterFragment = FlutterFragment.withCachedEngine("my_engine_id")
                .build();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_container, flutterFragment)
                .commit();

        MethodChannel nativeChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "com.example.flutter/native");
        nativeChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                switch (methodCall.method) {
                    case "jumpToNative":
                        // 跳转原生页面
                        Intent jumpToNativeIntent = new Intent(FlutterPageByFlutterFragmentActivity.this, QRActivity.class);
                        startActivity(jumpToNativeIntent);
                        break;
                    default:
                        result.notImplemented();
                        break;
                }
            }
        });
    }

    @Override
    protected int setLayoutXmlid() {
        return R.layout.activity_flutter_by_fluuterview;
    }

    @Override
    public String getTag() {
        return "FlutterPageByFlutterViewActivity";
    }
}
